package pool;

public class TestPC2 {
	public static void main(String[] args) {
		Tv tv = new Tv();
		new Player(tv).start();
		new Watcher(tv).start();
	}
}

//生产者   表演者
class Player extends Thread {
	Tv tv;

	public Player(Tv tv) {
		this.tv = tv;
	}

	@Override
	public void run() {
		for (int i = 0; i < 10; i++) {
			if (i % 2 == 0) {
				this.tv.play("=腾讯视频=");
			} else {
				this.tv.play("=爱奇艺=");
			}
		}
	}
}

//消费者    观众
class Watcher extends Thread {

	Tv tv;

	public Watcher(Tv tv) {
		this.tv = tv;
	}

	@Override
	public void run() {
		for (int i = 0; i < 10; i++) {
			this.tv.watch();
		}

	}
}

//   节目
class Tv {
	String voice;
	boolean flag = true;

	//表演的方法
	public synchronized void play(String voice) {
		if (!flag) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println("表演了节目" + voice);
		//通知其他线程观看
		this.notifyAll();
		//		更新节目
		this.voice = voice;
		this.flag = !this.flag;
	}

	//观看节目的方法
	public synchronized void watch() {
		if (flag) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println("观看了节目" + voice);
		this.notifyAll();
		this.flag = !this.flag;
	}
}